﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Job.Web.Framework;
using Job.Entity;
using Job.Logic.Personal;

namespace Job.Web.userControls.Personal
{
    public partial class EditPassword : BaseCtrl
    {
        private int JobAccountId
        {
            get
            {
                MySession session = new MySession();
                SessionPersonal person = session.GetPersonalFromSession();
                return person.PersonAccountid;
            }
        }

        private int errorId = myR["errorid"].IntValue;

        PersonalLogic pl = new PersonalLogic();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (errorId == 1)
                {
                    string message = "修改密码成功！请使用新密码登录。";
                    ShowMessage(message);
                }
            }
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            JobPersonAccount account = pl.GetAccountByAccountid(JobAccountId);
            if (!CheckOldPassword(account))
            {
                ShowMessage("旧密码输入错误，请重新输入。");
                return;
            }
            account.Password = txtNewPWD.Text.Trim();

            string message = string.Empty;
            try
            {
                pl.EditPassword(account);
                string url = string.Format("{0}Person/PersonManager.aspx?categoryid=1&ctrlId=7&errorid={1}", WebCommon.GetApplicationPath(Request), 1);
                Response.Redirect(url);    
            }
            catch(Exception ex)
            {
                message = "修改密码失败，请查看失败信息："+ex.Message;
            }
            
            
        }

        private bool CheckOldPassword(JobPersonAccount account)
        {
            
            if (account != null)
            {
                if (Functions.EncryptPassword(account.Password) == Functions.EncryptPassword(txtOldPWD.Text.Trim()))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }

        private void ShowMessage(string message)
        {
            this.msgTd.Visible = true;
            this.msgTd.InnerHtml = "<font color='#990000'>" + message + "</font>";
            this.msgTd.Height = "30px";
            this.msgTd.VAlign = "middle";
        }
    }
}